# -*- coding:utf-8 -*-
import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("划龙舟比赛")

# 颜色定义
BLUE = (64, 164, 223)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)


# 龙舟类
class DragonBoat:
    def __init__(self):
        self.width = 100
        self.height = 40
        self.x = 100
        self.y = HEIGHT // 2
        self.speed = 5
        self.score = 0

    def draw(self):
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))
        # 画龙头
        pygame.draw.polygon(screen, RED, [(self.x + self.width, self.y),
                                          (self.x + self.width + 30, self.y + self.height // 2),
                                          (self.x + self.width, self.y + self.height)])

    def move(self, keys):
        if keys[pygame.K_UP] and self.y > 0:
            self.y -= self.speed
        if keys[pygame.K_DOWN] and self.y < HEIGHT - self.height:
            self.y += self.speed
        if keys[pygame.K_LEFT] and self.x > 0:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width:
            self.x += self.speed


# 障碍物类
class Obstacle:
    def __init__(self):
        self.width = 30
        self.height = random.randint(50, 150)
        self.x = WIDTH
        self.y = random.randint(0, HEIGHT - self.height)
        self.speed = random.randint(3, 7)

    def draw(self):
        pygame.draw.rect(screen, GREEN, (self.x, self.y, self.width, self.height))

    def move(self):
        self.x -= self.speed
        if self.x < -self.width:
            return True
        return False


# 游戏主循环
def game_loop():
    clock = pygame.time.Clock()
    boat = DragonBoat()
    obstacles = []
    obstacle_timer = 0
    font = pygame.font.SysFont(None, 36)
    game_over = False

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        keys = pygame.key.get_pressed()
        boat.move(keys)

        # 生成障碍物
        obstacle_timer += 1
        if obstacle_timer > 60:  # 每60帧生成一个新障碍
            obstacles.append(Obstacle())
            obstacle_timer = 0

        # 移动障碍物并检测碰撞
        for obstacle in obstacles[:]:
            if obstacle.move():
                obstacles.remove(obstacle)
                boat.score += 1

            # 碰撞检测
            if (boat.x < obstacle.x + obstacle.width and
                    boat.x + boat.width > obstacle.x and
                    boat.y < obstacle.y + obstacle.height and
                    boat.y + boat.height > obstacle.y):
                game_over = True

        # 绘制
        screen.fill(BLUE)
        boat.draw()
        for obstacle in obstacles:
            obstacle.draw()

        # 显示分数
        score_text = font.render(f"分数: {boat.score}", True, WHITE)
        screen.blit(score_text, (10, 10))

        pygame.display.flip()
        clock.tick(60)

    # 游戏结束显示
    game_over_text = font.render(f"游戏结束! 最终分数: {boat.score}", True, WHITE)
    screen.blit(game_over_text, (WIDTH // 2 - 150, HEIGHT // 2))
    pygame.display.flip()
    pygame.time.wait(3000)


if __name__ == "__main__":
    game_loop()
